package com.boot4g.admin.module.sys.controller;

import com.alibaba.fastjson.JSON;
import com.boot4g.entity.SysUserEntity;
import com.boot4g.security.core.SecurityUser;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Controller公共组件
 * 
 * @author chenjinjie
 * @email guangyaoliangzijie@163.com
 * @date 2016年11月9日 下午9:42:26
 */
public abstract class AbstractController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	protected SecurityUser getUser() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		if (principal instanceof SecurityUser) {

			return (SecurityUser) principal;

		} else {
			//不能直接转换 需要通过json转码
			String string = JSON.toJSON(principal).toString();
			SecurityUser securityUser = JSON.parseObject(string, SecurityUser.class);
			return securityUser;
		}
	}

	protected Long getUserId() {
		return getUser().getUserId();
	}

	protected Long getDeptId() {
		return getUser().getGroupId();
	}
}
